package com.ankicoo.database.configuration;

import com.ankicoo.database.constant.DataSourceConst;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class DataSourceAspect {
    @Pointcut("@annotation(com.ankicoo.database.configuration.DataSource) || @within(com.ankicoo.database.configuration.DataSource)")
    public void dataSourcePointCut() {
    }

    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        DynamicDatasource.name.set(DataSourceConst.DATA_SOURCE_CENTER);
        Object proceed = point.proceed();
        DynamicDatasource.name.set(DataSourceConst.DATA_SOURCE_DEFAULT);
        return proceed;
    }


}
